package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.PermissionMapper;
import com.atguigu.ssyx.acl.mapper.RolePermissionMapper;
import com.atguigu.ssyx.acl.service.PermissionService;
import com.atguigu.ssyx.acl.utils.PermissionHelper;
import com.atguigu.ssyx.model.acl.Permission;
import com.atguigu.ssyx.model.acl.RolePermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ClassName: PermissionServiceImpl
 * Package: com.atguigu.ssyx.acl.service.impl
 * Description:
 *
 * @Author Root
 * @Create 2023/9/7 14:17
 * @Version 1.0
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    //查询所有菜单
    // @Override
    public List<Permission> queryAllPermission() {
        //1查询所有菜单
        List<Permission> allPermissionList = baseMapper.selectList(null);
        //2转换要求数据格式
        List<Permission> result = PermissionHelper.buildPermission(allPermissionList);

        return result;
    }


    //递归删除菜单
    @Override
    public void removeChileById(Long id) {
        //idList有删除所有菜单id
        List<Long> idList = new ArrayList<>();

        //根据当前菜单id，获取当前菜单下面所有子菜单
        //如果子菜单下面还有子菜单,都要获取到
        //递归找到当前菜单子菜单
        this.getAllPermissionId(id, idList);

        //加上当前菜单
        idList.add(id);
        baseMapper.deleteBatchIds(idList);
    }

    @Override
    public Map<String, Object> getPermissionByRoleId(Long roleId) {
        //所有菜单
        List<Permission> allPermissionList = baseMapper.selectList(null);
        List<Permission> buildAllPermissionList = PermissionHelper.buildPermission(allPermissionList);
        //根据roleId查询菜单
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getRoleId, roleId);
        List<RolePermission> rolePermissionList = rolePermissionMapper.selectList(queryWrapper);
        //获取已分配的菜单的菜单id
        List<Long> permissionIdList = rolePermissionList.stream().map(item -> {
            return item.getPermissionId();
        }).collect(Collectors.toList());
        //封装角色已分配的菜单对象
        List<Permission> assignPermissionList = new ArrayList<>();
        for (Permission permission : buildAllPermissionList) {
            if (permission.getPid() == 0) {
                //父节点，顶层
                if (permissionIdList.contains(permission.getId())) {
                    assignPermissionList.add(permission);
                    permission.setSelect(true);
                }
                this.setPermissionChildren(permission.getChildren(), permissionIdList);
            }
        }
        Map<String,Object> result=new HashMap<>();
        result.put("allPermissions",buildAllPermissionList);
        result.put("assignPermissions",assignPermissionList);
        return result;

    }

    private void setPermissionChildren(List<Permission> children, List<Long> permissionIdList) {
        for (Permission permission : children) {
            if (permissionIdList.contains(permission.getId())) {
                permission.setSelect(true);
            }
            this.setPermissionChildren(permission.getChildren(), permissionIdList);
        }
    }

    private void getAllPermissionId(Long id, List<Long> idList) {
        //根据当前菜单id查询子菜单id
        LambdaQueryWrapper<Permission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Permission::getPid, id);
        List<Permission> childList = baseMapper.selectList(wrapper);
        //递归查询是否还有子菜单
        childList.stream().forEach(item -> {
            //封装菜单id到list集合
            idList.add(item.getId());
            //递归
            this.getAllPermissionId(item.getId(), idList);
        });
    }
}
